﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SundayWishModel.Entities;
using NHibernate;
using NHibernate.Criterion;

namespace SundayWishBLL
{
    public class PlanHelper
    {
        public virtual IList<Plan> GetPlanByUserId(int UserId)
        {
            ISession Session = NHibernateHelper.GetSession();
            UserHelper uh = new UserHelper();
            return uh.GetUser(UserId).Plans;
        }

        public virtual IList<Plan> GetUnSendPlanByUserId(int UserId)
        {
            ISession Session = NHibernateHelper.GetSession();
            return   Session.CreateCriteria(typeof(Plan))
                .Add(Restrictions.Eq("IsSend", 0))
                .Add(Restrictions.Eq("UserId", UserId))
                .AddOrder(Order.Desc("SendDate"))
                .List<Plan>();               
        }

        public virtual Plan GetPlan(int PlanId)
        {
            ISession Session = NHibernateHelper.GetSession();
            return Session.Get<Plan>(PlanId);
        }


        public IList<Plan> GetUser()
        {
            ISession Session = NHibernateHelper.GetSession();
            return Session.CreateQuery("from Plan").List<Plan>();
        }


        public virtual Boolean AddPlan(Plan plan)
        {
            ISession Session = NHibernateHelper.GetSession();
            int count = (Session.CreateSQLQuery("select * from UserPlan where SendDate = :sd and UserId = :ui")
                    .SetString("sd", plan.SendDate.ToString())
                    .SetString("ui", plan.UserId.ToString()).List<Object>()).Count;
            if (count > 0) return false;
            Session.Save(plan);
            Session.Flush();
            return true;
        }

        public virtual Boolean DeletePlan(Plan plan)
        {
            ISession Session = NHibernateHelper.GetSession();
            using(ITransaction Transaction = Session.BeginTransaction())
            {
                try
                {
                    Session.Delete(plan);
                    Session.Flush();
                    Session.CreateSQLQuery("Delete From Plan_Dish where PlanId = :pi")
                    .SetString("pi", plan.PlanId.ToString());
                    Transaction.Commit();
                }
                catch (HibernateException)
                {
                    Transaction.Rollback();
                    return false;
                }
            }
            return true;
        }

        public virtual Boolean UpdatePlan(Plan plan)
        {
            ISession Session = NHibernateHelper.GetSession();
            Session.Update(plan);
            Session.Flush();
            return true;
        }

        public virtual Boolean AddDishToPlan(int DishId, int PlanId)
        {
            Dish dish = (new PrivateDishHelper()).GetDish(DishId);
            Plan plan = GetPlan(PlanId);
            if (plan.Dishes.Contains<Dish>(dish)) return false;
            plan.Dishes.Add(dish);
            UpdatePlan(plan);
            return true;
        }

        public virtual Boolean DeleteDishFromPlan(int DishId, int PlanId)
        {
            ISession Session = NHibernateHelper.GetSession();
            Session.CreateSQLQuery("Delete From Plan_Dish where PlanId = :pi and DishId = :di")
            .SetParameter("pi", PlanId.ToString())
            .SetParameter("di", DishId.ToString()).ExecuteUpdate();
            Session.Flush();
            return true;
        }

        public virtual Boolean SendPlan(Plan plan)
        {
            ISession Session = NHibernateHelper.GetSession();
            plan.IsSend = 1;
            Session.Save(plan);
            Session.Flush();
            IList<Dish> DishList=Session.CreateSQLQuery("Select * From Plan_Dish pd,Dish d where pd.PlanId = pi and pd.DishId = d.DishId")
                .SetString("pi", plan.PlanId.ToString())
                .List<Dish>();
            foreach (Dish dish in DishList)
            {
                dish.DishCount++;
                Session.Update(dish);
                Session.Flush();
            }
            return true;
        }
    }
}
